package com.all.play.myapplication.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.MotionEvent;

import com.all.play.myapplication.R;
import com.all.play.myapplication.utils.SharedPreferencesUtil;

/**
 * Created by 聪明一只哈 on 2018/4/18.
 * Time: 2018/4/18  11:59
 */

public class SplashActivity extends AppCompatActivity {


    private Handler handler=new Handler();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        //延时跳转
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                SwitchPageChange();

            }
        },3000);


    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        SwitchPageChange();

        return super.onTouchEvent(event);
    }

    /**
     * 页面切换
     */
    private void SwitchPageChange() {
        if(isFirstLogin()){
            startActivity(new Intent(SplashActivity.this, LoginActivity.class));

        }else{
            startActivity(new Intent(SplashActivity.this, MainActivity.class));
            this.overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
        }
        finish();
    }

    /**
     * 判断是否是第一次登录
     * @return
     */
    private boolean isFirstLogin() {
        String name = SharedPreferencesUtil.getString(SplashActivity.this, "name", "");
        String pass = SharedPreferencesUtil.getString(SplashActivity.this, "pwd", "");
        if(!TextUtils.isEmpty(name)&&!TextUtils.isEmpty(pass)){
            return false;
        }

        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);



    }
}
